package com.pd.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pd.backend.dtos.PagePdAuthUserDto;
import com.pd.backend.dtos.PdAuthUserDto;
import com.pd.backend.dtos.PdAuthUserUpdateDto;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.entity.PdCoreStation;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.PdAuthUserMapper;
import com.pd.backend.pojo.PdAuthUser;
import com.pd.backend.service.PdAuthUserService;
import com.pd.backend.vo.PdAuthUserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户管理业务层实现类
 * @Auther : 菜鸡编程
 * @Date:2023/1/14
 * @Description:
 * @VERSON:1.8
 */
@Service
@Slf4j
@Transactional
public class PdAuthUserServiceImpl implements PdAuthUserService {


    @Autowired
    private PdAuthUserMapper pdAuthUserMapper;
    /**
     * 用户管理:分页模糊查询
     * @param pagePdAuthUserDto
     * @return
     */
    @Override
    public ResponseResult authUserPage(PagePdAuthUserDto pagePdAuthUserDto) {
        //校验参数
        if (Objects.isNull(pagePdAuthUserDto)) {
            //请求参数为空,返回错误消息:无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer page = pagePdAuthUserDto.getPage();
        Integer size = pagePdAuthUserDto.getSize();
        String account = pagePdAuthUserDto.getAccount();
        Long orgId = pagePdAuthUserDto.getOrgId();
        Date startTime = pagePdAuthUserDto.getStartTime();
        Date endTime = pagePdAuthUserDto.getEndTime();

        //开始分页
        PageHelper.startPage(page,size);

        //调用接口查询用户数据
        List<PdAuthUserVo> pdAuthUserVoList = pdAuthUserMapper.authUserPage(account,orgId,startTime,endTime);
        if (Objects.isNull(pdAuthUserVoList)) {
            //数据为空,返回错误消息:数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //获取分页后的所有数据
        PageInfo<PdAuthUserVo> pdCoreStationPageInfo = new PageInfo<>(pdAuthUserVoList);
        //返回数据
        return ResponseResult.okResult(pdCoreStationPageInfo);
    }


    /**
     * 新增用户
     * @param pdAuthUserDto
     * @return
     */
    @Override
    public ResponseResult authUserAdd(PdAuthUserDto pdAuthUserDto) {
        //校验参数
        if (Objects.isNull(pdAuthUserDto)) {
            //参数为空,返回错误消息:缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //用户性别和状态不能为空
        if (StringUtils.isBlank(pdAuthUserDto.getSex())) {
            //性别为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        if (Objects.isNull(pdAuthUserDto.getStatus())) {
            //状态为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        //调用接口新增用户
        boolean result = pdAuthUserMapper.authUserAdd(pdAuthUserDto);
        if (!result) {
            //新增失败.返回错误消息:新增失败
            return ResponseResult.errorResult(AppHttpCodeEnum.DATE_INSERT_ERROR);
        }
        //新增成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

<<<<<<< HEAD
    @Override
    public ResponseResult<List<PdAuthUser>> selectAllCourier() {
        List<PdAuthUser> pdAuthUsers = pdAuthUserMapper.selectAllCourier();
        return ResponseResult.okResult(pdAuthUsers);
=======
    /**
     * 根据用户id查询用户
     * @param id
     * @return
     */
    @Override
    public ResponseResult selectById(Long id) {
        //校验参数(无需校验)
        //处理业务
        //调用接口查询用户
        PdAuthUser pdAuthUser = pdAuthUserMapper.selectById(id);
        if (Objects.isNull(pdAuthUser)) {
            //数据为空.返回错误消息:数据状态异常
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_STATUS_EX);
        }
        return ResponseResult.okResult(pdAuthUser);
    }


    /**
     * 根据用户id修改用户
     * @param
     * @return
     */
    @Override
    public ResponseResult updateById(PdAuthUserUpdateDto pdAuthUserUpdateDto) {
        //校验参数
        if (Objects.isNull(pdAuthUserUpdateDto)) {
            //参数为空,返回错误消息:数据状态异常
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_STATUS_EX);
        }
        //判断性别及状态是否存在
        Integer status = pdAuthUserUpdateDto.getStatus();
        String sex = pdAuthUserUpdateDto.getSex();
        if (StringUtils.isBlank(sex)) {
            //性别为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        if (Objects.isNull(status)) {
            //状态为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        //调用接口修改用户
        boolean result = pdAuthUserMapper.updateById(pdAuthUserUpdateDto);

        if (!result) {
            //修改失败,返回错误消息:修改失败
            return ResponseResult.errorResult(AppHttpCodeEnum.DATE_UPDATE_ERROR);
        }
        //修改成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
>>>>>>> zhuhuanxin-01
    }
}
